//	--------------------------------------------------------------------
//	Copyright(C) 2006,2007 Zhao Yukun. All rights reserved.
//	This file is a part of the Xeres system.
//	Xeres 2007
//	Contact : xeres.engine@gmail.com
//	$(reservedInfo)
//	$(reservedDeclaration)
//	--------------------------------------------------------------------
///	@file	<CTexture2D9.h>
///	@path	~/src/graphics/dx9/
///	@date	2007/12/13
///	@desc	Base implementation of texture in DX 9.

#pragma once

#include "xeres.h"

#include "graphics/ITexture.h"
#include "graphics/dx9/CDevice9.h"

namespace xeres {

	/*!
		\class	CTexture2D9
		\brief	Base implementation of managed texture in DX 9.
	*/
	class CTexture2D9 : public ITexture
	{
	public:

		/// \ctor
		CTexture2D9( IDirect3DTexture9 * pTexture9 );

		/// \dtor
		virtual ~CTexture2D9( void );

		DECL_IOBJECT_CLASS( CTexture2D9 , ITexture );

	public:

	//@ interface

		/// \impl
		virtual bool OnLostDevice( void );

		/// \impl
		virtual bool OnResetDevice( RefWeak<IDevice> device );

		/// \impl
		virtual TextureType GetType( void );

		/// \impl
		virtual DXFormat GetFormat( void );

		/// \impl
		virtual Pair<int> GetSize( void );

		/// \impl
		virtual size_t GetDepth( void );

		/// \impl
		virtual bool IsWritable( void );

		/// \impl
		virtual bool IsReadable( void );

		/// \impl
		virtual bool Map( uint sub , ResourceMap& data );

		/// \impl
		virtual bool ReadBack( uint sub , ResourceMap& data , bool readonly );

		/// \impl
		virtual bool Unmap( uint sub );

		/// \impl
		virtual bool Update( uint sub , const Box<uint>& size , const byte * data , uint row_pitch , uint depth_pitch );

		/// \impl
		virtual IUnknown * GetTextureInterface( void );

	//@ local

		/// \brief		Get texture interface.
		IDirect3DTexture9 * GetTexture( void ) const { return m_pTexture9; }

	protected:

	//@ data

		IDirect3DTexture9 *		m_pTexture9;
		D3DSURFACE_DESC			m_desc;
	};

	/*! 
		\class	CTexture2DSurface9
		\brief	A handle of texture, holds textures from surface.
	*/
	class CTexture2DSurface9 : public CTexture2D9
	{
	public:

		/// \ctor
		CTexture2DSurface9( IDirect3DSurface9 * surface );

		/// \dtor
		virtual ~CTexture2DSurface9( void );

		DECL_IOBJECT_CLASS( CTexture2DSurface9 , CTexture2D9 );

	protected:

		/// \brief	Get texture from surface.
		static IDirect3DTexture9 * GetTextureFromSurface( IDirect3DSurface9 * surface );
	};

} // namespace xeres
